package cn.ncu.wechat.controller;

import cn.ncu.wechat.service.CouponRecordService;
import cn.ncu.wechat.service.CouponService;
import cn.ncu.wechat.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/admin/coupons")
public class couponController {
    @Autowired
    private CouponService couponService;

    @Autowired
    private CouponRecordService couponRecordService;

    @GetMapping("")
    public Result getCouponList(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer pageSize,
                                @RequestParam Integer status){
        return couponService.getCouponList(page, pageSize, status);
    }

    @PostMapping("")
    public Result addOrUpdateCoupon(@RequestBody Map<String, Object> request) {
        return couponService.addOrUpdateCoupon(request);
    }

    @GetMapping("/{id}/records")
    public Result distributeCouponRecord(@PathVariable Long id,
                                         @RequestParam(defaultValue = "1") Integer page,
                                         @RequestParam(defaultValue = "10") Integer pageSize,
                                         @RequestParam Integer status){
        return couponRecordService.distributeCouponRecord(id, page, pageSize, status);
    }

    @PostMapping("/{id}/batch-issue")
    public Result sendCouponByArray(@PathVariable Long id, @RequestBody Map<String, Object> request) {
        return couponRecordService.sendCouponByArray(id, request);
    }

    @PostMapping("/{id}/void")
    public Result voidCoupon(@PathVariable Long id, @RequestBody Map<String, String> request) {
        return couponService.voidCoupon(id, request);
    }

    @GetMapping("/{id}/usage-records")
    private Result<?> getUsageRecordsByOpenId(@PathVariable String id,@RequestParam Integer page,@RequestParam Integer pageSize,@RequestParam Integer status){
        Integer uid = Integer.parseInt(id);
        if (uid==null){
            return Result.fail(400,"参数错误");
        }
        return couponService.getUsageRecordsByOpenId(uid,page,pageSize,status);
    }

    @GetMapping("/statistics")
    public Result<?> couponStatistics(@RequestParam String startDate, @RequestParam String endDate){
        return couponService.couponStatistics(startDate, endDate);
    }

}
